//
//  ScoureCell.m
//  Estate
//
//  Created by heyong on 16/7/23.
//  Copyright © 2016年 ttsx. All rights reserved.
//

#import "ScoureCell.h"

@implementation ScoureCell
- (void)setIsEdit:(BOOL)isEdit{

        self.textFile.userInteractionEnabled=isEdit;
        self.subBtn.userInteractionEnabled=isEdit;
        self.addBtn.userInteractionEnabled=isEdit;

}
- (void)awakeFromNib {
    [super awakeFromNib];
    self.textFile.backgroundColor = [UIColor colorWithRed:1.00 green:0.99 blue:0.92 alpha:1.00];
    self.selectionStyle=UITableViewCellSelectionStyleNone;
    [self setAddandSub];
    
//    if (!self.scoreLimit) {
//        self.scoreLimit = @"100";
//    }
}

- (void)setScoreLimit:(NSString *)scoreLimit {
    if (!scoreLimit) {
        _scoreLimit = @"100";
    } else {
        _scoreLimit = scoreLimit;
    }
}

- (void)setAddandSub{
    self.textFile.delegate=self;
    self.title.textColor=UIColorFromRGB(0x787878);
    self.title.font=[UIFont systemFontOfSize:16*BIZ];
    self.backView.backgroundColor=[TTTheme defaultManger].selectColor;
    self.textFile.textAlignment=1;
    self.textFile.keyboardType=UIKeyboardTypeNumberPad;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

}

- (IBAction)subAction:(UIButton *)sender {
    NSInteger scoure=[self.textFile.text integerValue];
    if(scoure>0){
        scoure--;
    } else {
        [LSUtility showToastWithMessage:@"请输入一个大于0的数" inView:self.view Andtime:1.];
        scoure = 0;
    }
    self.textFile.text=[NSString stringWithFormat:@"%li",scoure];
    if(self.textBlock){
        self.textBlock([NSString stringWithFormat:@"%li",scoure]);
    }
}

- (IBAction)addAction:(UIButton *)sender {
    NSInteger scoure = [self.textFile.text integerValue];
    if (scoure < [self.scoreLimit integerValue]) {
        scoure++;
        
    } else {
        NSString *str = [NSString stringWithFormat:@"最高限制为%@分", self.scoreLimit];
        [LSUtility showToastWithMessage:str inView:self.view Andtime:1.];
        scoure = [self.scoreLimit integerValue];
    }
    self.textFile.text = [NSString stringWithFormat:@"%li",scoure];
    if(self.textBlock){
        self.textBlock([NSString stringWithFormat:@"%li",scoure]);
    }
}

- (IBAction)textChange:(UITextField *)sender {
    NSString *str = [LSUtility getTheCorrectNum:sender.text];
    NSInteger scoure = [str integerValue];
    
    if (scoure > [self.scoreLimit integerValue]) {
        NSString *str = [NSString stringWithFormat:@"最高限制为%@分", self.scoreLimit];
        [LSUtility showToastWithMessage:str inView:self.view Andtime:1.];
        scoure = [self.scoreLimit integerValue];

//        self.textFile.text = [NSString stringWithFormat:@"%@", self.scoreLimit];
    } else if (scoure < 0) {
        [LSUtility showToastWithMessage:@"请输入一个大于0的数" inView:self.view Andtime:1.];
        scoure = 0;
    }
    sender.text = [NSString stringWithFormat:@"%li", scoure];
    
    NSString *regex = @"^[1-9]\\d*$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL isMatch = [pred evaluateWithObject:str];
    
    if(self.textBlock){
        if(isMatch){
            self.textBlock([NSString stringWithFormat:@"%li",scoure]);
        }else{
            self.textBlock(@"0");
        }
    }
}

//设置分割线
- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor); CGContextFillRect(context, rect);
    //上分割线，
//    CGContextSetStrokeColorWithColor(context,[UIColor colorWithRed:0.89 green:0.89 blue:0.90 alpha:1.00].CGColor); CGContextStrokeRect(context, CGRectMake(0, -1, rect.size.width , 1));
    //下分割线
    CGContextSetStrokeColorWithColor(context,[UIColor colorWithRed:0.89 green:0.89 blue:0.90 alpha:1.00].CGColor);
    CGContextStrokeRect(context, CGRectMake(0, rect.size.height, rect.size.width, 1));
}

@end


